package com.qiangqiang.task.server.mapper.provider;

import com.qiangqiang.task.server.entity.UserInfo;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-01-30 20:50
 **/
public class UserMapperProvider extends BaseProvider{
    /**
     * @param [map]
     * @return java.lang.String
     * @author sky
     * @date 2018/7/14
     * @describe: 将插入和更新写在一起
     */
    public String upSert(final Map<String, Object> map) {
        UserInfo userInfo = (UserInfo) map.get("userInfo");
        StringBuffer sb = new StringBuffer(generateInsertSQL(UserInfo.class)).append(" ON DUPLICATE KEY UPDATE ");
        if (userInfo.getNickName() != null) {
            sb.append("nick_name=#{userInfo.nickName},");
        }
        if (userInfo.getSex() != null) {
            sb.append("sex=#{userInfo.sex},");
        }
        if (userInfo.getSchool() != null) {
            sb.append("school=#{userInfo.school},");
        }
        if (userInfo.getCampus() != null) {
            sb.append("campus=#{userInfo.campus},");
        }
        if (userInfo.getDormitory() != null) {
            sb.append("dormitory=#{userInfo.dormitory},");
        }
        if (userInfo.getHeadImgUrl() != null) {
            sb.append("head_img_url=#{userInfo.headImgUrl},");
        }
        if (userInfo.getUpdateTime() != null) {
            sb.append("update_time=#{userInfo.updateTime},");
        }
        if (userInfo.getEmail() != null) {
            sb.append("email=#{userInfo.email},");
        }
        if (userInfo.getPassword() != null) {
            sb.append("password=#{userInfo.password},");
        }
        if (userInfo.getPhone() != null && !"".equals(userInfo.getPhone().trim())) {
            sb.append("phone=#{userInfo.phone}");
        }
        return sb.toString();
    }

    public String queryAllUser(final Map<String, Object> map) {
        final int page = (int) map.get("page");
        final int pageSize = (int) map.get("pageSize");

        SQL sql = new SQL() {
            {
                SELECT("*");
                FROM(" user_info ");
                //这里写条件
            }
        };
        StringBuffer sb = new StringBuffer(sql.toString());
        sb.append(getLimit(page, pageSize));
        return sb.toString();
    }

    public String queryNamesByIds(final Map<String, Object> map) {
        List<Long> ids = (List<Long>) map.get("userIds");

        StringBuffer sb = new StringBuffer("select nick_name from user_info where user_id in (");
        sb.append(String.join(",", ids.stream().
                map(String::valueOf).collect(Collectors.toList()))).append(")");
        return sb.toString();
    }

    public String queryNickNameDTOByIds(final Map<String, Object> map) {
        List<Long> ids = (List<Long>) map.get("userIds");
        StringBuffer sb = new StringBuffer("select user_id,nick_name from user_info where user_id in (");
        sb.append(String.join(",", ids.stream().
                map(String::valueOf).collect(Collectors.toList()))).append(")");
        return sb.toString();
    }
}
